package com.txzt.gaea.business.ident.service.impl;

import com.txzt.gaea.business.ident.domain.model.IdentInfo;
import com.txzt.gaea.business.ident.service.IIdentInfoService;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.common.enums.DeclareStatus;
import com.txzt.gaea.common.enums.FlowBizStatusEnum;
import com.txzt.gaea.common.enums.FlowFlagEnum;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.exception.CustomException;
import com.txzt.gaea.flow.model.FlowBiz;
import com.txzt.gaea.flow.model.FlowBizStep;
import com.txzt.gaea.flow.service.FlowService;
import com.txzt.gaea.vo.AdminUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IdentInfoServiceImpl extends BaseServiceImpl<IdentInfo> implements IIdentInfoService {


    @Autowired
    private FlowService flowService;


    @Override
    public IdentInfo selectIdentInfoById(Long id) {
        IdentInfo identInfo = sqlManager.single(IdentInfo.class,id);
        if (null == identInfo) {
            return null;
        }
//        List<FlowBizStepVo> list = flowService.getFlowBizStep(identInfo.getFlowId());
//        identInfo.setFlowStep(list);
//        identInfo.setCheck(TranslateUtil.getCheck(list));
        //翻译项目组长
        identInfo.setProjectLeaderName(TranslateUtil.getUserName(identInfo.getProjectLeader()));
        return identInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int submitIdentInfo(IdentInfo identInfo) {
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        identInfo.setDeclareStatus(DeclareStatus.CHECK.getCode());
        identInfo.setDeptId(adminUserVO.getDeptid());
        if (null == identInfo.getId()){
            identInfo.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
            sqlManager.insertTemplate(identInfo,true);
        }
        if (null == identInfo.getFlowId()){
            //开启流程
            Long flowBizId = flowService.startFlowUpgrade(identInfo.getId(), "tb_ident_info","declare_status",
                    DeclareStatus.BACK.getCode(), DeclareStatus.PASS.getCode(), DeclareStatus.REJECT.getCode() , DeclareStatus.INIT.getCode(),
                    FlowFlagEnum.APPRAISAL_MANAGEMENT.getCode(),adminUserVO.getDeptid());
            identInfo.setFlowId(flowBizId);
        }else {
            FlowBiz flowBiz = flowService.getFlowBiz(identInfo.getFlowId());
            if (flowBiz.getCurrentStatus().equals(FlowBizStatusEnum.BACK.getCode())){
                // 若项目状态为结题评审 - 退回修改，需执行流程的退回修改的下一步操作
                flowService.nextStepOnlyForBack(identInfo.getFlowId());
            }
        }
        return sqlManager.updateTemplateById(identInfo);
    }

    @Override
    public int revocationIdent(IdentInfo identInfo) {
        if (!identInfo.getDeclareStatus().equals(DeclareStatus.CHECK.getCode())) {
            throw new CustomException("非审核中数据不可撤回！");
        }
        List<FlowBizStep> stepList = sqlManager.lambdaQuery(FlowBizStep.class)
                .andEq(FlowBizStep::getFlowBizId, identInfo.getFlowId())
                .orderBy("step_no asc").select();
        if (0 == stepList.size()) {
            throw new CustomException("撤回异常！");
        }else {
            if (null == stepList.get(0).getStepStatus()) {
                IdentInfo target = new IdentInfo();
                target.setId(identInfo.getId());
                target.setDeclareStatus(DeclareStatus.INIT.getCode());
                return sqlManager.updateTemplateById(target);
            }else {
                throw new CustomException("数据已审核不可撤回！");
            }
        }
    }

    /**
     * 查询待审核数量(鉴定管理)
     * @param identInfo
     * @return
     */
    @Override
    public Long listCheckCount(IdentInfo identInfo) {
        List<Long> list = sqlManager.select("identInfo.selectIdentInfoCheckCount", Long.class, identInfo);
        return list.get(0);
    }

}
